package com.pg.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pg.entity.EipTemp;
import com.pg.entity.dd.EipProductionOrder;
import com.pg.entity.dd.EipWorkOrder;
import com.pg.entity.dd.OrderView;
import com.pg.mapper.EipProductionOrderMapper;
import com.pg.mapper.EipTempMapper;
import com.pg.mapper.EipWorkOrderMapper;
import com.pg.service.EipWorkOrderService;
import com.pg.utils.ColumnUtil;
import com.pg.utils.DataStringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * 工单 服务实现类
 * </p>
 *
 * @author likl
 * @since 2020-07-06
 */
@Service
@DS("proxyDs")
@Primary
public class EipWorkOrderServiceImpl extends ServiceImpl<EipWorkOrderMapper, EipWorkOrder> implements EipWorkOrderService {

    @Autowired
    private EipTempMapper eipTempMapper;

    @Autowired
    private EipProductionOrderMapper eipProductionOrderMapper;

    @Autowired
    private EipWorkOrderMapper eipWorkOrderMapper;

    @Override
    public void updateWorkOrder(EipTemp eipTemp) {
        LambdaQueryWrapper<EipTemp> eipTempQueryWrapper = new LambdaQueryWrapper<>();
        if (eipTemp.getLinkPUID() != null) {
            eipTempQueryWrapper.eq(EipTemp::getLinkPUID, eipTemp.getLinkPUID());
        }
        EipTemp eipTempByLinkPUID = eipTempMapper.selectOne(eipTempQueryWrapper);
        if (eipTempByLinkPUID == null) {
            eipTemp.setPuid(UUID.randomUUID().toString());
            eipTempMapper.insert(eipTemp);
        } else {
            eipTemp.setPuid(eipTempByLinkPUID.getPuid());
            eipTempMapper.updateByPuid(eipTemp);
        }
        EipWorkOrder eipWorkOrder = new EipWorkOrder();
        eipWorkOrder.setIoflag("mod");
        LambdaQueryWrapper<EipWorkOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EipWorkOrder::getPuid, eipTemp.getLinkPUID());
        eipWorkOrderMapper.update(eipWorkOrder, queryWrapper);
    }

    @Override
    public void logicalDelete(String puid) {
        eipWorkOrderMapper.logicalDelete(puid);
    }

    @Override
    public void logicalDeleteBatch(List<String> asList) {
        for (String puid : asList) {
            eipWorkOrderMapper.logicalDelete(puid);
        }
    }

    @Override
    public EipWorkOrder selectByPuid(String puid) {
        EipWorkOrder eipWorkOrder = eipWorkOrderMapper.selectById(puid);
        eipWorkOrder.setRemark1(selectWarnMessage(eipWorkOrder));
        return eipWorkOrder;
    }

    @Override
    public EipWorkOrder selectByWoNo(String woNo) {
        LambdaQueryWrapper<EipWorkOrder> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(woNo)) {
            queryWrapper.eq(EipWorkOrder::getWoNo, woNo);
        }
        return eipWorkOrderMapper.selectOne(queryWrapper);
    }

    @Override
    public List<OrderView> selectListByMap(Map<String, String> map) {
        return eipWorkOrderMapper.selectListByMap(map.get("modifyTime"), map.get("ioflag"));
    }

    @Override
    public IPage<EipWorkOrder> selectForList(Page<EipWorkOrder> page, EipWorkOrder eipWorkOrder, Date planStartDateBegin, Date planStartDateFinish) {
        LambdaQueryWrapper<EipWorkOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EipWorkOrder::getIsDeleted, "0");
        //全库唯一码
        if (StringUtils.isNotEmpty(eipWorkOrder.getPuid())) {
            queryWrapper.eq(EipWorkOrder::getPuid, eipWorkOrder.getPuid());
        }
        //工单号
        if (StringUtils.isNotEmpty(eipWorkOrder.getWoNo())) {
            queryWrapper.like(EipWorkOrder::getWoNo, eipWorkOrder.getWoNo());
        }
        //生产订单号
        if (StringUtils.isNotEmpty(eipWorkOrder.getIpoNo())) {
            queryWrapper.like(EipWorkOrder::getIpoNo, eipWorkOrder.getIpoNo());
        }
        //种类编码
        if (StringUtils.isNotEmpty(eipWorkOrder.getSubclassCode())) {
            queryWrapper.like(EipWorkOrder::getSubclassCode, eipWorkOrder.getSubclassCode());
        }
        //工艺路线编码
        if (StringUtils.isNotEmpty(eipWorkOrder.getProcessRouteNo())) {
            queryWrapper.like(EipWorkOrder::getProcessRouteNo, eipWorkOrder.getProcessRouteNo());
        }
        //厂家物料描述
        if (StringUtils.isNotEmpty(eipWorkOrder.getMaterialsDescription())) {
            queryWrapper.like(EipWorkOrder::getMaterialsDescription, eipWorkOrder.getMaterialsDescription());
        }
        //计划日期(起)
        if (planStartDateBegin != null) {
            queryWrapper.ge(EipWorkOrder::getPlanStartDate, planStartDateBegin);
        }
        //计划日期(止)
        if (planStartDateFinish != null) {
            queryWrapper.le(EipWorkOrder::getPlanStartDate, planStartDateFinish);
        }
        //工单状态
        if (StringUtils.isNotEmpty(eipWorkOrder.getWoStatus())) {
            queryWrapper.like(EipWorkOrder::getWoStatus, eipWorkOrder.getWoStatus());
        }
        if (StringUtils.isNotEmpty(eipWorkOrder.getPurchaserHqCode())) {
            queryWrapper.eq(EipWorkOrder::getPurchaserHqCode, eipWorkOrder.getPurchaserHqCode());
        }
        queryWrapper.orderByDesc(EipWorkOrder::getPlanStartDate);
        queryWrapper.orderByAsc(EipWorkOrder::getWoNo);
        List<EipWorkOrder> eipWorkOrderListTemp = new ArrayList<>();
        IPage<EipWorkOrder> eipWorkOrderPage = eipWorkOrderMapper.selectPage(page, queryWrapper);
        for (EipWorkOrder eipWorkOrderTemp : eipWorkOrderPage.getRecords()) {

            String simpleName = DataStringUtil.getSimpleName(eipWorkOrderTemp.getMaterialsDescription());
            eipWorkOrderTemp.setMaterialsDescription(simpleName);
            eipWorkOrderTemp.setRemark1(selectWarnMessage(eipWorkOrderTemp));
            eipWorkOrderListTemp.add(eipWorkOrderTemp);
        }
        eipWorkOrderPage.setRecords(eipWorkOrderListTemp);
        return eipWorkOrderPage;
    }

    private String selectWarnMessage(EipWorkOrder eipWorkOrder) {
        boolean flag = true;
        Map<String, Object> map = new HashMap<String, Object>();
        String ipoNo = eipWorkOrder.getIpoNo();
        map.put(ColumnUtil.getName(EipWorkOrder::getIpoNo), ipoNo);
        map.put(ColumnUtil.getName(EipWorkOrder::getIsDeleted), "0");
        List<EipProductionOrder> eipProductionOrders = eipProductionOrderMapper.selectByMap(map);
        if (!CollectionUtils.isEmpty(eipProductionOrders)) {
            flag = false;
        }
        String warnMessage = flag ? "10001" : "";

        Date planFinishDate = eipWorkOrder.getPlanFinishDate();
        Date actualFinishDate = eipWorkOrder.getActualFinishDate();
        if (planFinishDate != null && actualFinishDate != null) {
            if (planFinishDate.before(actualFinishDate)) {
                warnMessage = "".equals(warnMessage) ? "10003" : warnMessage + ";10003";
            } else if (planFinishDate.after(actualFinishDate)) {
                warnMessage = "".equals(warnMessage) ? "10004" : warnMessage + ";10004";
            } else {
                warnMessage = "".equals(warnMessage) ? "10005" : warnMessage + ";10005";
            }
        }
        return warnMessage;
    }
}
